<template>
  <div>
    <el-card>
      <template #header>
        <div class="df jcsb">
          <span>店铺管理 </span>
          <el-button
            :type="!isEidt ? 'primary' : 'success'"
            @click="handleEdit"
            >{{ !isEidt ? "编辑" : "保存" }}</el-button
          >
        </div>
      </template>
      <el-form
        :model="fromData"
        label-width="80px"
        class="form"
        :disabled="!isEidt"
      >
        <el-form-item label="店铺名称">
          <el-input v-model="fromData.name"> </el-input>
        </el-form-item>
        <el-form-item label="店铺公告">
          <el-input type="textarea" :rows="5" v-model="fromData.bulletin">
          </el-input>
        </el-form-item>
        <el-form-item label="店铺头像">
          <el-upload
            class="avatar-uploader"
            action="http://sell.h5.itsource.cn:8087/shop/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="fromData.avatar" :src="fromData.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="店铺图片">
          <el-upload
            action="http://sell.h5.itsource.cn:8087/shop/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleShop"
            :file-list="fromData.pics"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="配送费">
          <el-input-number
            v-model="fromData.deliveryPrice"
            :min="1"
            :max="1000"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="配送时间">
          <el-input v-model="fromData.deliveryTime"></el-input>
        </el-form-item>
        <el-form-item label="配送描述">
          <el-input v-model="fromData.description"></el-input>
        </el-form-item>
        <el-form-item label="店铺评分" class="df aic shopScore">
          <el-rate
            v-model="fromData.score"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="销量">
          <el-input v-model="fromData.sellCount"></el-input>
        </el-form-item>
        <el-form-item label="活动">
          <el-checkbox-group
            v-if="fromData.supports"
            v-model="fromData.supports"
          >
            <el-checkbox label="在线支付满28减5"></el-checkbox>
            <el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
            <el-checkbox label="单人精彩套餐"></el-checkbox>
            <el-checkbox label="特价饮品8折抢购"></el-checkbox>
            <el-checkbox label="单人特色套餐"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="营业时间">
          <el-time-picker
            is-range
            v-model="fromData.date"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            value-format="yyyy-MM-dd hh:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { api_shop_edit, api_shop_info } from "@/api/shop";
export default {
  data() {
    return {
      fromData: {},
      dialogImageUrl: "",
      dialogVisible: false,
      // 选中编辑
      isEidt: false,
      value: 3.7,
    };
  },
  methods: {
    async getShopInfo() {
      let res = await api_shop_info();
      let score = res.data.data.score / 20;
      let pics = res.data.data.pics.map((v) => {
        return {
          url: v,
        };
      });
      this.fromData = { ...res.data.data, pics, score };
    },
    handleShop(res) {
      this.fromData.pics.push({
        url: res.imgUrl,
      });
    },
    handleAvatarSuccess(res, flie) {
      this.fromData.avatar = res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = /(png|jpeg)/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG PNG格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    async handleEdit() {
      this.isEidt = !this.isEidt;
      if (!this.isEidt) {
        let pics = this.fromData.pics.map((v) => v.url);
        let score = this.fromData.score * 20;

        let res = await api_shop_edit({ ...this.fromData, pics, score });
        this.getShopInfo();
      }
    },
  },
  created() {
    this.getShopInfo();
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 400px;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.shopScore {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;
  }
}
</style>